package com.huipinche.test;
import com.huipinche.dao.bean.Journey;
import com.huipinche.dao.bean.User;
import com.huipinche.dao.impl.JourneyDaoimpl;
import com.huipinche.dao.impl.UserDaoimpl;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.List;
import java.util.Scanner;
public class daotest {
    User user1 = new User();
    User user2 = new User();
    Journey journey1=new Journey();
    Journey journey2=new Journey();
    Journey journey3=new Journey();
  //  Date date1;
    /**
     * 初始化方法：
     *  用于资源申请，所有测试方法在执行之前都会先执行该方法
     */
    @Before
    public void init() throws ParseException {
        System.out.println("init...初始化2两个用户类和三个行程类");
        System.out.println("\n");

        //初始化两个用户变量
        user1.setUser("lyh");
        user1.setPassword("123456");
        user1.setTel("18826681399");
        user1.setJfid(99);
        user1.setId(96);
        user2.setUser("xlj");
        user2.setPassword("666666");
        user2.setTel("666666");
        user2.setJfid(6);
        user2.setId(77);

        //初始化两个行程变量
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = dateFormat.parse("2021-08-08 21:00:00");
        Date date2 = dateFormat.parse("2021-07-07 21:00:00");
        Date date3 = dateFormat.parse("2021-06-06 21:00:00");

        journey1.setJourneyflag(1);
        journey1.setStartadd("华南理工大学国际校区西北面");
        journey1.setEndadd("新造A1口");
        journey1.setTel("18826681399");
        journey1.setUid(96);
        journey1.setJourneytime(date1);

        journey2.setJourneyflag(0);
        journey2.setStartadd("新造A1口");
        journey2.setEndadd("华南理工大学国际校区西北面");
        journey2.setTel("18826681399");
        journey2.setUid(77);
        journey2.setJourneytime(date2);


        journey3.setJourneyflag(2);
        journey3.setStartadd("新造B1口");
        journey3.setEndadd("华南理工大学国际校区南面");
        journey3.setTel("18826681399");
        journey3.setUid(77);
        journey3.setJourneytime(date3);
    }

    //测试Userdao层的函数
    @Test
    public void UserDaotest() {
        UserDaoimpl userDaoimpl =new UserDaoimpl();
        //写入第一个类
        userDaoimpl.insertUser(user1);
        //写入第二个类
        userDaoimpl.insertUser(user2);
        User testuser = userDaoimpl.getUserByName("xlj");
        System.out.print("写入了两个用户，查询用户名字为xlj"+ testuser.toString());
    }

    //测试JourneyDao的函数
    @Test
    public void JourneyDaotest() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date01 =  dateFormat.parse("2021-06-06 21:00:00");//dateFormat.parse("2021-08-08 21:00:00");

        JourneyDaoimpl journeyDaoimpl = new JourneyDaoimpl();
        //测试写入数据
        journeyDaoimpl.insertJourney(journey1);
        journeyDaoimpl.insertJourney(journey2);
        journeyDaoimpl.insertJourney(journey3);

        //测试查找所有数据
        List<Journey> testjourney= journeyDaoimpl.seletJourneyAll();
        System.out.print("查询所有订单"+testjourney.toString());
        System.out.print("\n");

        //测试根据id删除数据
//        journeyDaoimpl.deleteJourney(1);
//        System.out.print("删除 id 为 1 的订单");
//        System.out.print("\n");

        //通过出发地址查询数据
        List<Journey> testjourney1 = journeyDaoimpl.seletJourneyByStartadd("新造B1口");
        System.out.print("通过地址查询所有订单"+testjourney1.toString());
        System.out.print("\n");


        //通过时间查询数据
        List<Journey> testjourney2 = journeyDaoimpl.seletJourneyByTime(date01);
        System.out.print("通过时间查询所有订单"+testjourney2.toString());

    }


    @Test
    public void data() throws ParseException {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1=new Date();
        String s1=sdf.format(d1);
        System.out.println(s1);
        System.out.println("\n");

        //String——>Date
        String s2="1999-12-12 13:20:01";
        Date d2=sdf.parse(s2);
        System.out.println(d2);

    }




    /**
     * 释放资源方法：
     *  在所有测试方法执行完后，都会自动执行该方法
     */
    @After
    public void close(){
        System.out.println("close...");
    }

//
//    public static void main(String[] args) {
//
//    }

}
